﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;

namespace KPIS.GERP.GHRMS.DAL
{
    public class TrainingInstituteDAL : ITrainingInstituteDAL
    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, ConfigurationSettings.AppSettings["ConnectionString"].ToString());
        ConvertNullable cn = new ConvertNullable();

        public IList<TrainingInstituteInfo> GetFull()
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<TrainingInstituteInfo> infoList = new List<TrainingInstituteInfo>();

            try 
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT ti.training_ins_seq, ti.training_ins_descr, ti.training_ins_responsible"
                    + ", ti.record_status, mc.country_name, mc.country_seq"
                    + " FROM hrm_training_institutes ti"
                    + " Left Join mas_countries mc ON ti.country_seq = mc.country_seq"
                    + " WHERE ti.record_status not like @delete_status";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        TrainingInstituteInfo info = new TrainingInstituteInfo();
                        info.training_ins_seq = Convert.ToInt32(drc[i]["training_ins_seq"]);
                        info.training_ins_descr = drc[i]["training_ins_descr"].ToString();
                        info.training_ins_responsible = drc[i]["training_ins_responsible"].ToString();
                        info.country_seq = Convert.ToInt32(drc[i]["country_seq"]);
                        info.country_name = drc[i]["country_name"].ToString();

                        infoList.Add(info);                       

                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }
        public TrainingInstituteInfo GetData(int training_ins_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            TrainingInstituteInfo info = new TrainingInstituteInfo();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@training_ins_seq", training_ins_seq);

                strSQL = "SELECT ti.training_ins_seq, ti.training_ins_descr, ti.training_ins_responsible"
                    + ", ti.record_status, mc.country_name, mc.country_seq"
                    + " FROM hrm_training_institutes ti"
                    + " Left Join mas_countries mc ON ti.country_seq = mc.country_seq"
                    + " WHERE ti.record_status not like @delete_status"
                    + (training_ins_seq > 0 ? " AND scholar_fund_seq = @training_ins_seq " : "");

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {                    
                    info.training_ins_seq = Convert.ToInt32(drc[0]["training_ins_seq"]);
                    info.training_ins_descr = drc[0]["training_ins_descr"].ToString();
                    info.training_ins_responsible = drc[0]["training_ins_responsible"].ToString();
                    info.country_seq = Convert.ToInt32(drc[0]["country_seq"]);
                    info.country_name = drc[0]["country_name"].ToString();                   
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }
        public void Insert(TrainingInstituteInfo info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@training_ins_descr", info.training_ins_descr);
                dbManager.AddParameters(1, "@country_seq", info.country_seq);
                dbManager.AddParameters(2, "@record_status", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(3, "@created_by", info.created_by);
                dbManager.AddParameters(4, "@created_when", info.created_when);
                dbManager.AddParameters(5, "@training_ins_responsible", info.training_ins_responsible);

                strSQL = "INSERT INTO hrm_training_institutes"
                    + "(training_ins_descr,training_ins_responsible, country_seq, record_status"
                    + ", created_by, created_when)"
                    + " VALUES (@training_ins_descr"
                    + ", @training_ins_responsible"
                    + ", @country_seq"
                    + ", @record_status"
                    + ", @created_by"
                    + ", @created_when)";
                
                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch(Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Update(TrainingInstituteInfo info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@training_ins_seq", info.training_ins_seq);
                dbManager.AddParameters(1, "@training_ins_descr", info.training_ins_descr);
                dbManager.AddParameters(2, "@country_seq", info.country_seq);
                dbManager.AddParameters(3, "@updated_by", info.updated_by);
                dbManager.AddParameters(4, "@updated_when", info.updated_when);
                dbManager.AddParameters(5, "@training_ins_responsible", info.training_ins_responsible);

                strSQL = "UPDATE hrm_training_institutes"
                    + " SET training_ins_descr = @training_ins_descr"
                    + ", training_ins_responsible = @training_ins_responsible"
                    + ", country_seq = @country_seq"
                    + ", updated_by = @updated_by"
                    + ", updated_when = @updated_when"
                    + " WHERE training_ins_seq = @training_ins_seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Delete(TrainingInstituteInfo info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@training_ins_seq", info.training_ins_seq);
                dbManager.AddParameters(1, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@updated_by", info.updated_by);
                dbManager.AddParameters(3, "@updated_when", info.updated_when);

                strSQL = "UPDATE hrm_training_institutes"
                    + " SET record_status = @record_status"
                    + ", updated_by = @updated_by"
                    + ", updated_when = @updated_when"
                    + " WHERE training_ins_seq = @training_ins_seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
